
template <typename T> 
T profile2::get(string name) const
{
	trim(name);
	map_type::const_iterator i = _items.find(name);
	if (i == _items.end())
		throw not_found(name);
	i->second.used = true;
	std::istringstream in(i->second.str);
	in.copyfmt(_parser);
	in.clear();
	in.exceptions(0);
	T value;
	in >> value;
	if (in.fail() || in.bad() || !in.eof())
		throw parse_error(name, i->second.str);
	return value;
}

template <> 
inline std::string profile2::get<std::string>(string name) const
{
	trim(name);
	map_type::const_iterator i = _items.find(name);
	if (i == _items.end())
		throw not_found(name);
	i->second.used = true;
	return i->second.str;
}

template <typename T> 
T profile2::get(string name, const T & def) const
{
	try {
		return get<T>(name);
	} catch(error &) {
		std::ostringstream out;
		out << def;
		_items[name] = value(out.str(), true);
		return def;
	}
}
	
template <typename T> 
bool profile2::query(string name, T & value) const
{	
	try {
		value = get<T>(name);
		return true;
	} catch(not_found &) {
		return false;
	}
}

template <typename T> 
bool profile2::query(string name, T & value, const T & def) const
{
	try {
		return query(name, value);
	} catch(parse_error &) {
		value = def;
		std::ostringstream out;
		out << def;
		_items[name] = profile2::value(out.str(), true);
		return true;
	}
}
